﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.IRepository;
using DAL.Admin;
using Entities;

namespace WindowsForms.Admin
{
    public partial class FormBoPhan : Form
    {
        public IBoPhanRepository bp = new BLTBoPhanRepository();
        public FormBoPhan()
        {
            InitializeComponent();
            dataGridViewhienthibop.AutoGenerateColumns = false;
        }

        private void btrefresh_Click(object sender, EventArgs e)
        {
            dataGridViewhienthibop.DataSource = bp.LayTatCa();
        }

        private void btthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btthembophan_Click(object sender, EventArgs e)
        {
            FormThemBoPhan fraddbp = new FormThemBoPhan();
            fraddbp.FormClosed += new FormClosedEventHandler(fraddbp_FormClosed);
            fraddbp.ShowDialog();
        }

        void fraddbp_FormClosed(object sender, FormClosedEventArgs e)
        {
            btrefresh.PerformClick();
        }

        private void dataGridViewhienthibop_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {


                string ma = dataGridViewhienthibop.Rows[e.RowIndex].Cells[0].Value.ToString();
                BoPhan bphan = bp.LayTheoId(ma);
                if (e.ColumnIndex == 3)
                {


                    if (bphan != null)
                    {
                        bp.XoaBoPhan(bphan);
                        btrefresh.PerformClick();
                    }
                }
                if (e.ColumnIndex == 4)
                {
                    FormThemBoPhan frbp = new FormThemBoPhan();
                    FormThemBoPhan.bophan = bphan;
                    frbp.FormClosed += new FormClosedEventHandler(frbp_FormClosed);
                    frbp.ShowDialog();
                }
            }
            catch
            {


            }

        }

        void frbp_FormClosed(object sender, FormClosedEventArgs e)
        {
            btrefresh.PerformClick();
        }
    }
}
